import type { MallCouponTemplateApi } from '#/api/mall/promotion/coupon/couponTemplate';

import {
  CouponTemplateValidityTypeEnum,
  PromotionDiscountTypeEnum,
} from '@vben/constants';
import { floatToFixed2, formatDate } from '@vben/utils';
import { $t } from '#/locales';

/** 格式化【优惠金额/折扣】 */
export function discountFormat(row: MallCouponTemplateApi.CouponTemplate) {
  if (row.discountType === PromotionDiscountTypeEnum.PRICE.type) {
    return `¥${floatToFixed2(row.discountPrice)}`;
  }
  if (row.discountType === PromotionDiscountTypeEnum.PERCENT.type) {
    return `${row.discountPercent}%`;
  }
  return $t('views.mall.promotion.coupon.template.unknown', [row.discountType]);
}

/** 格式化【领取上限】 */
export function takeLimitCountFormat(
  row: MallCouponTemplateApi.CouponTemplate,
) {
  if (row.takeLimitCount) {
    if (row.takeLimitCount === -1) {
      return $t('views.mall.promotion.coupon.template.unlimitedTakeCount');
    }
    return $t('views.mall.promotion.coupon.template.limitedCount', [row.takeLimitCount]);
  } else {
    return ' ';
  }
}

/** 格式化【有效期限】 */
export function validityTypeFormat(row: MallCouponTemplateApi.CouponTemplate) {
  if (row.validityType === CouponTemplateValidityTypeEnum.DATE.type) {
    return $t('views.mall.promotion.coupon.template.validityTime-1', [formatDate(row.validStartTime), formatDate(row.validEndTime)]);
  }
  if (row.validityType === CouponTemplateValidityTypeEnum.TERM.type) {
    return $t('views.mall.promotion.coupon.template.validityTime-2', [row.fixedStartTerm, row.fixedEndTerm]);
  }
  return $t('views.mall.promotion.coupon.template.unknown', [row.validityType]);
}

/** 格式化【totalCount】 */
export function totalCountFormat(row: MallCouponTemplateApi.CouponTemplate) {
  if (row.totalCount === -1) {
    return $t('views.mall.promotion.coupon.record.unlimited');
  }
  return row.totalCount;
}

/** 格式化【剩余数量】 */
export function remainedCountFormat(row: MallCouponTemplateApi.CouponTemplate) {
  if (row.totalCount === -1) {
    return $t('views.mall.promotion.coupon.record.unlimited');
  }
  return row.totalCount - row.takeCount;
}

/** 格式化【最低消费】 */
export function usePriceFormat(row: MallCouponTemplateApi.CouponTemplate) {
  return `¥${floatToFixed2(row.usePrice)}`;
}
